﻿<?php
require_once 'header.php';
require_once 'common.php';

$obj_api = new Api;
$str_action = trim($_GET['act']);
$obj_api->$str_action();

class Api
{
    const SECRET_KEY = TOKEN_KEY;

    /**
     * 插入新纪录
     *
     * @author 丁勇
     * @param void
     * @return string
     */
    public function add_package(){

        $str_app_ids     = trim($_GET['app_ids']);
        $str_sign        = trim($_GET['sign']);
        $str_name        = trim($_GET['name']);
        $str_version     = trim($_GET['version']);
        $int_size        = intval($_GET['size']);
        $str_url         = trim($_GET['url']);
        $str_remark      = trim($_GET['remark']);
        $str_create_time = date('Y-m-d H:i:s');

        // Check params
        if ( empty($str_app_ids) || empty($str_name) || empty($str_version) || empty($str_url) || empty($int_size) ) {
            output_json_error(-10001, 'Parameter error.');
        }

        // Check sign
        $str_check_sign = md5(self::SECRET_KEY . $str_name . $str_url . $str_app_ids);

        if( $str_sign != $str_check_sign ) {
            output_json_error(-10002, 'Sign error.');
        }

        // Check app ids
        $arr_app_ids        = json_decode($str_app_ids, true);

        if ( ! is_array($arr_app_ids) ) {
            output_json_error(-10003, 'App ids error.');
        }

        $obj_db             = connect_mysql();

        // Check whether exists
        $str_sql            = 'SELECT count(1) AS total FROM ios_apps_package
                               WHERE url=:url ';
        $arr_binds          = array(
                                    ':url'     => $str_url,
                                    ':name'    => $str_name,
                                    ':version' => $str_version
                              );
        $arr_result         = query_mysql($obj_db, $str_sql, $arr_binds);

        if( $arr_result[0]['total'] ){
            output_json_error(-10005, '对不起，数据库中已存在你要插入的数据信息，操作失败！');
        }

        // Insert ios_apps_package
        $str_sql  = 'INSERT INTO ios_apps_package(' .
                    'name, ' .
                    'version, ' .
                    'size, ' .
                    'url, ' .
                    'remark, ' .
                    'status, ' .
                    'create_time, ' .
                    'update_time) ' .
                    'VALUES (' .
                    ':name, ' .
                    ':version, ' .
                    ':size, ' .
                    ':url, ' .
                    ':remark,' .
                    ':status,' .
                    ':create_time,' .
                    ':update_time)';

        $arr_binds = array(
                           ':name'        => $str_name,
                           ':version'     => $str_version,
                           ':size'        => $int_size,
                           ':url'         => $str_url,
                           ':remark'      => $str_remark,
                           ':status'      => 1,
                           ':create_time' => $str_create_time,
                           ':update_time' => $str_create_time
                     );
        $str_result = exec_mysql($obj_db, $str_sql, $arr_binds);

        if( $str_result ) {
            output_json_error(-10004, 'Insert error.');
        }

        $int_last_package_id = mysql_insert_id();

        // Insert ios_apps_package_detail
        foreach ( $arr_app_ids as $sub ) {
            $str_sql  = 'INSERT INTO ios_apps_package_detail(' .
                        'package_id, ' .
                        'app_id, ' .
                        'bundle_version, ' .
                        'is_main, ' .
                        'display_order, ' .
                        'create_time, ' .
                        'update_time) ' .
                        'VALUES (' .
                        ':package_id, ' .
                        ':app_id, ' .
                        ':bundle_version, ' .
                        ':is_main, ' .
                        ':display_order,' .
                        ':create_time,' .
                        ':update_time)';
            $arr_binds = array(
                               ':package_id'      => $int_last_package_id,
                               ':app_id'          => intval($sub['app_id']),
                               ':bundle_version'  => trim($sub['bundle_version']),
                               ':is_main'         => 1,
                               ':display_order'   => 1,
                               ':create_time'     => $str_create_time,
                               ':update_time'     => $str_create_time
                          );

            $str_result = exec_mysql( $obj_db, $str_sql, $arr_binds );
        }

        output_json_succ('success');
    }

    /**
     * 更新纪录
     *
     * @author dingyong
     * @date 2013-09-19
     * @param void
     * @return void
     */
    public function update_package(){

        $str_app_ids     = trim($_GET['app_ids']);
        $str_sign        = trim($_GET['sign']);
        $str_name        = trim($_GET['name']);
        $str_version     = trim($_GET['version']);
        $int_size        = intval($_GET['size']);
        $str_url         = trim($_GET['url']);
        $str_remark      = trim($_GET['remark']);
        $str_create_time = date('Y-m-d H:i:s');

        // Check params
        if ( empty($str_app_ids) || empty($str_name) || empty($str_version) || empty($str_url) || empty($int_size) ) {
            output_json_error(-10006, 'Parameter error.');
        }

        // Check sign
        $str_check_sign = md5( self::SECRET_KEY . $str_name . $str_url . $str_app_ids );

        if( $str_sign != $str_check_sign ) {
            output_json_error(-10007, 'Sign error.');
        }

        // Check app ids
        $arr_app_ids        = json_decode($str_app_ids, true);

        if ( ! is_array($arr_app_ids) ) {
            output_json_error(-10008, 'App ids error.');
        }

        $obj_db             = connect_mysql();

        // Check whether exists
        $str_sql            = 'SELECT package_id FROM ios_apps_package
                               WHERE url=:url';
        $arr_binds          = array(
                                    ':url'     => $str_url,
                                    ':name'    => $str_name,
                                    ':version' => $str_version
                              );
        $arr_result         = query_mysql($obj_db, $str_sql, $arr_binds);
        $int_package_id     = $arr_result[0]['package_id'];

        if( $int_package_id<=0 ){
            output_json_error(-10009,'Not exist error.');
        }

        //update package
        $str_sql   = 'UPDATE ios_apps_package SET
                      name=:name, version=:version, size=:size,
                      remark=:remark, status=:status, update_time=:update_time
                      WHERE package_id=:package_id ';
        $arr_binds = array(
                            ':name'         => $str_name,
                            ':version'      => $str_version,
                            ':size'         => $int_size,
                            ':remark'       => $str_remark,
                            ':status'       => 1,
                            ':update_time'  => $str_create_time,
                            ':package_id'   => $int_package_id
                     );
        $str_result = exec_mysql($obj_db,$str_sql,$arr_binds);

        if( $str_result ){
            output_json_error(-10010, '数据库中不存在你要更新的记录，更新失败！');
        }

        //update package_detail
        $bool_success             = true;

        foreach( $arr_app_ids as $sub ){
            $str_sql          = 'UPDATE ios_apps_package_detail SET
                                 bundle_version=:bundle_version, update_time=:update_time
                                 WHERE package_id=:package_id
                                 AND app_id=:app_id';
            $arr_binds        = array(
                                      ':bundle_version' => trim($sub['bundle_version']),
                                      ':update_time'    => $str_create_time,
                                      ':package_id'     => $int_package_id,
                                      ':app_id'         => intval($sub['app_id'])
                                 );
            $str_exec_result  = exec_mysql($obj_db, $str_sql, $arr_binds);

            if( $str_exec_result ){
                $bool_success = false;
            }
        }

        if( !$bool_success ){
            output_json_error(-10011, 'Update package detail error.');
        }

        output_json_succ('success');
    }


    /**
     * 鑾峰彇app鍒楄〃
     *
     * @param  void
     * @return void
     * @author 鏉庨懌杈?
     */
    public function get_app_list() {

        $str_sign       = trim($_GET['sign']);
        $str_check_sign = md5(self::SECRET_KEY);

        // Check
        if ( $str_sign != $str_check_sign ) {
            output_json_error(-20001, 'Sign error.');
        }

        // Get info
        $obj_db = connect_mysql();

        $str_sql = 'SELECT t1.APP_URL FROM ios_apps_info t1
                    LEFT JOIN ios_apps_dsp t2 ON(t1.DSP_ID=t2.DSP_ID)
                    LEFT JOIN ios_apps_package_detail t3 ON(t1.ID=t3.app_id)
                    WHERE t2.FILE_ID = 10000 AND
                    t1.STATUS = 1 AND
                    t3.app_id is null
                    LIMIT 1000';

        $arr_result = query_mysql($obj_db, $str_sql);

        $arr_info = array();
        if ( ! empty($arr_result) ) {
            foreach ($arr_result as $sub) {
                $arr_info[] = $sub['APP_URL'];
            }
        }

        output_json_succ($arr_info);
    }
}